package com.zh.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import com.zh.mapper.CourseDao;
import com.zh.mapper.PersonDao;
import com.zh.po.CoursePo;
import com.zh.po.PersonPo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class TestService2 {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    public CourseDao courseDao;


    //todo 如果用若依的多数据源方式,则需要将查询/处理的一次操作单独用@DataSource进行标记
    @Resource
    public PersonDao personDao;



    @DataSource(DataSourceType.SLAVE)
    public void testDataChange(){

        System.out.println(DynamicDataSourceContextHolder.getDataSourceType());
        PersonPo po = new PersonPo();
        po.setId(UUID.randomUUID().toString());
        po.setName("张辉1");
        po.setSex(1);
        personDao.insert(po);
    }
    @DataSource(DataSourceType.MASTER)
    public void  testDataChange2(){
        System.out.println(DynamicDataSourceContextHolder.getDataSourceType());
        QueryWrapper<CoursePo> cQuery = new QueryWrapper();
        cQuery.lambda().eq(CoursePo::getCid,"01");
        CoursePo coursePo = courseDao.selectOne(cQuery);
        logger.info("当前课程{}",coursePo.getCname());
    }

}
